package edu.lhh;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.lhh.dao.newstypedao;
import edu.lhh.model.newstype;

/**
 * Servlet implementation class search
 */
@WebServlet(urlPatterns="/search")
public class search extends HttpServlet {
	private static final long serialVersionUID = 1L;
	newstypedao dao = new newstypedao();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public search() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String nameORid = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

		newstype tp = null;
		List<newstype> lst = new ArrayList<newstype>();

		try {
			lst = dao.findbyidORname(nameORid);
			tp = lst.get(0);
			if(tp.getAge() != null){
				request.setAttribute("types", lst);
				request.getRequestDispatcher("news_type.jsp").forward(request, response);

			}else{
				response.sendRedirect("null.jsp");

			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
